package com.iwhalecloud.bss.kite.cucc.order.accept.chain.impl;

import com.iwhalecloud.bss.kite.client.dto.cust.ContactDTO;
import com.iwhalecloud.bss.kite.client.dto.cust.CustDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.cucc.order.accept.chain.AbstractOrderHandler;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderContactInfo;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItemAttr;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOrderItemAttr;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.iwhalecloud.bss.kite.order.client.dto.OrderContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import com.ztesoft.bss.order.base.consts.IKeyValues;
import com.ztesoft.bss.order.base.consts.KeyValues;
import com.ztesoft.bss.order.base.consts.KeyValuesLocal;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 联系人人处理，只考虑主场景的联系人信息
 */
@Component
public class OrderContactInfoHandler extends AbstractOrderHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void doHandle(OrderRequest request) {
        KiteAttr kiteAttr = querySceneInstService.getCommonAttr(request.getMainSceneInstId(), KeyConsts.CONTACT_INFO);
        if (ObjectUtils.isEmpty(kiteAttr)) {
            return;
        }
        if (KiteStringUtils.isEmpty(kiteAttr.getValue())) {
            return;
        }
        this.createOrderContactInfo(request, kiteAttr);
    }

    private void createOrderContactInfo(OrderRequest request, KiteAttr kiteAttr) {
        OrderContactInfo orderContactInfo = initContactInfo(request, kiteAttr);
        if (ObjectUtils.isEmpty(orderContactInfo)) {
            return;
        }
        orderContactInfo.setOrderContactInfoId(SeqUtils.getNext(TableConsts.OrderContactInfo));
        orderContactInfo.setCustOrderId(request.getCustOrderId());
        orderContactInfo.setCustId(request.getCustId());
        orderContactInfo.setCreateStaff(request.getStaffId());
        orderContactInfo.setOrderItemId(request.getCustOrderId());
        orderContactInfo.setStatusCd(CoreDataInteract.EVT_C_0014_1100);
        orderContactInfo.setOrderType(KeyValues.ORDER_TYPE_CUSTORDER);
        orderContactInfo.setRelationTypeCd(KeyValuesLocal.PARTY_TYPE_003);
        orderContactInfo.setChangeTimes("0");
        orderContactInfo.setStatusDate(request.getAcceptDate());
        orderContactInfo.setPartyType(KeyValuesLocal.PARTY_TYPE_005);
        orderContactInfo.setFinishTime(request.getAcceptDate());
        orderContactInfo.setCreateDate(request.getAcceptDate());
        orderContactInfo.setUpdateDate(request.getAcceptDate());
        // 组装
        List<OrderContactInfo> orderContactInfos = new ArrayList<>();
        orderContactInfos.add(orderContactInfo);
        request.getOrderContainer().getOrderContactInfoMap().put(request.getCustOrderId(), orderContactInfos);

        // 订单项属性设置联系人信息
        setContactInfoAttr(request, orderContactInfo.getContactName(), orderContactInfo.getContactPhone());

    }

    private void setContactInfoAttr(OrderRequest request, String contactName, String contactphone) {
        // 设置联系人姓名
        KiteOrderItemAttr contactNameAttr = new KiteOrderItemAttr();
        contactNameAttr.setFieldName("contact_name");
        contactNameAttr.setPFieldName("busi_bookDate");
        contactNameAttr.setAttrValue(contactName);

        // 设置联系人号码
        KiteOrderItemAttr contactPhoneAttr = new KiteOrderItemAttr();
        contactPhoneAttr.setFieldName("contact_phone");
        contactPhoneAttr.setPFieldName("busi_bookDate");
        contactPhoneAttr.setAttrValue(contactphone);

        request.getOrderContainer().getOrderItemMap().forEach((orderItemId, orderItem) -> {
            List<OrderItemAttr> orderItemAttrs = new ArrayList<>();
            orderItemAttrs.add(getOrderItemAttr(contactNameAttr, orderItem));
            orderItemAttrs.add(getOrderItemAttr(contactPhoneAttr, orderItem));
            OrderContainer.ofRequest(request).addOrderItemAttrs(orderItemAttrs);
        });
    }

    private OrderContactInfo initContactInfo(OrderRequest request, KiteAttr kiteAttr) {
        ContactDTO contactInfo = getContactDTO(request.getCustId(), kiteAttr.getValue());
        if (!ObjectUtils.isEmpty(contactInfo)) {
            OrderContactInfo orderContactInfo = new OrderContactInfo();
            orderContactInfo.setContactName(contactInfo.getContactName());
            orderContactInfo.setContactPhone(contactInfo.getMobilePhone());
            return orderContactInfo;
        }
        else {
            ContactDTO newContactInfo = getContactDTO(request.getCurrentCustId(), kiteAttr.getValue());
            if (!ObjectUtils.isEmpty(newContactInfo)) {
                OrderContactInfo orderContactInfo = new OrderContactInfo();
                orderContactInfo.setContactName(newContactInfo.getContactName());
                orderContactInfo.setContactPhone(newContactInfo.getMobilePhone());
                return orderContactInfo;
            }
        }
        return null;
    }

    private ContactDTO getContactDTO(String custId, String contactId) {
        CustDTO custDTO = OrderContext.getCust(custId);
        if (KiteListUtils.isNotEmpty(custDTO.getContacts())) {
            ContactDTO contactInfo = custDTO.getContacts().stream()
                .filter(t -> contactId.equals(t.getContactId())).findAny().orElse(null);
            return contactInfo;
        }
        return null;
    }

    private OrderItemAttr getOrderItemAttr(KiteOrderItemAttr kiteOrderItemAttr, OrderItem orderItem) {
        OrderItemAttr orderItemAttr = new OrderItemAttr();
        orderItemAttr.setFieldName(kiteOrderItemAttr.getFieldName());
        orderItemAttr.setpFieldName(
            KiteStringUtils.isNotEmpty(kiteOrderItemAttr.getPFieldName()) ? kiteOrderItemAttr.getPFieldName()
                : kiteOrderItemAttr.getFieldName());
        orderItemAttr.setAttrId(
            KiteStringUtils.isNotEmpty(kiteOrderItemAttr.getAttrId()) ? kiteOrderItemAttr.getAttrId() : "-1");
        orderItemAttr.setAttrValue(kiteOrderItemAttr.getAttrValue());
        orderItemAttr.setOrderId(orderItem.getCustOrderId());
        orderItemAttr.setOrderItemId(orderItem.getOrderItemId());
        orderItemAttr.setCustId(orderItem.getBelongCustId());
        orderItemAttr.setCreateStaff(orderItem.getCreateStaff());
        orderItemAttr.setCreateDate(orderItem.getCreateDate());
        orderItemAttr.setStatusDate(orderItemAttr.getCreateDate());
        orderItemAttr.setUpdateDate(orderItemAttr.getCreateDate());
        orderItemAttr.setpOrderAttrId(orderItemAttr.getAttrId());
        orderItemAttr.setAttrCd(kiteOrderItemAttr.getAttrId());
        orderItemAttr.setOrderType(IKeyValues.ORDER_TYPE_ORDERITEM);
        orderItemAttr.setStatusCd(CoreDataInteract.EVT_C_0014_1000);
        orderItemAttr.setVerNum("0");
        orderItemAttr.setOrderItemAttrId(SeqUtils.getNext(TableConsts.OrderItemAttr));
        return orderItemAttr;
    }
}
